﻿using System;
using System.Collections.Generic;
using System.Text;
using GroupChat2013.Entity;
using GroupChat2013.Common;
using System.IO;
using GroupChat2013.Service;

namespace GroupChat2013.Logic
{
    /// <summary>
    /// 解析配置基类
    /// </summary>
    public abstract class BaseConfigParse
    {
        /// <summary>
        /// 解析
        /// </summary>
        /// <returns>按钮信息数组</returns>
        public abstract List<ButtonConfigInfo> Parse();

        /// <summary>
        /// 下载按钮图标
        /// </summary>
        /// <param name="config">按钮信息</param>
        protected void DownLoadButtonImage(ButtonConfigInfo config)
        {
            string imageDic = Path.Combine(EngineContext.Current.FilePathManager.PersonConfigPath, ConstClass.ConfigImage);
            Tools.CreateDir(imageDic);

            config.imgNormal = SetButtonImagePath(config.imgNormal, imageDic);
        }

        /// <summary>
        /// 设置按钮图标路径
        /// </summary>
        /// <param name="url">路径</param>
        /// <param name="imageDic">图标文件夹</param>
        /// <returns></returns>
        private string SetButtonImagePath(string url, string imageDic)
        {
            string imageName = string.Empty;
            string imagePath = string.Empty;

            if (string.IsNullOrEmpty(url))
                url = string.Empty;
            else
            {
                imageName = GetImageName(url);
                imagePath = Path.Combine(imageDic, imageName);
                if (File.Exists(imagePath))
                    url = imagePath;
                else
                {
                    url = string.Format("http://{0}{1}", ServiceUrlProxy.GetInstance().m_BaseUrl, url);
                    url = FileManage.SaveFileForUrl(url, imagePath);
                }
            }

            return url;
        }

        /// <summary>
        /// 获取图标名字
        /// </summary>
        /// <param name="url">路径</param>
        /// <returns>图标名字</returns>
        private string GetImageName(string url)
        {
            if (string.IsNullOrEmpty(url))
                return string.Empty;

            return url.Substring(url.LastIndexOf('/') + 1);
        }
    }
}
